﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ECI.Entities;
using ECI.CommonLibrary;
using System.Data ;

namespace ECI.DynamicData.Data
{
    public class DataModelFromXML
    {
        public static EciTable GetDataModel(string code)
        {
            DataSet ds = GetDataSetFromXML(code);
            DataTable dtTable = ds.Tables["PROGRAM"];
            DataTable dtField = ds.Tables["ELEMENT"];
            DataTable dtFunction = ds.Tables["FUNCTION"];
            DataTable dtControl = ds.Tables["CONTROL"];
            DataTable dtConfirm = ds.Tables["CONFIRM"];
            DataTable dtDataSource = ds.Tables["DATA_SOURCE"];
            DataTable dtGroup = ds.Tables["GROUP"];
 
            Entities.Builder.EntityBuilder<EciTable, EciTableEnum> tableBuilder = new ECI.Entities.Builder.EntityBuilder<EciTable, EciTableEnum>();
            Entities.Builder.EntityBuilder<EciField, EciFieldEnum> fieldBuilder = new ECI.Entities.Builder.EntityBuilder<EciField, EciFieldEnum>();
            Entities.Builder.EntityBuilder<EciTableControl, EciTableControlEnum> controlBuilder = new ECI.Entities.Builder.EntityBuilder<EciTableControl, EciTableControlEnum>();
            Entities.Builder.EntityBuilder<EciTableFunction, EciTableFunctionEnum> functionBuilder = new ECI.Entities.Builder.EntityBuilder<EciTableFunction, EciTableFunctionEnum>();
            Entities.Builder.EntityBuilder<EciTableConfirm, EciTableConfirmEnum> confirmBuilder = new ECI.Entities.Builder.EntityBuilder<EciTableConfirm, EciTableConfirmEnum>();
            Entities.Builder.EntityBuilder<EciTableDataSource, EciTableDataSourceEnum> dataSourceBuilder = new ECI.Entities.Builder.EntityBuilder<EciTableDataSource, EciTableDataSourceEnum>();
            Entities.Builder.EntityBuilder<EciTableGroup, EciTableGroupEnum> groupBuilder = new ECI.Entities.Builder.EntityBuilder<EciTableGroup, EciTableGroupEnum>();

            EciTable table = tableBuilder.GetEntity(dtTable);

            if (table.HasFetchData)
            {

                List<EciField> listFields = fieldBuilder.GetEntities(dtField);

                foreach (EciField item in listFields)
                {
                    item.EciTable = table;
                    if (item.BatchUpdateShow == 1) table.ListBatchEdit.Add(item);
                    if (item.PrimaryKey == 1) table.PrimaryKey = item;
                    if (item.SumField == 1) table.ListSum.Add(item);
                    if (item.GridShow == 1 && item.LinkUrl.ToMyString() != "") table.ListShowLink.Add(item);
                    if (item.GridShow == 1 && item.DictionaryTypeConfig.ToUpper().StartsWith("HTML")) table.ListShowHTML.Add(item);
                    if (item.GridShow == 1 && item.ShowTip == "Y") table.ListShowTIP.Add(item);
                }

                var queryShowItems = from c in listFields where c.QueryShow.ToMyString() == "1" orderby c.QuerySeq select c;
                table.ListField = queryShowItems.ToList<EciField>();

                var gridShowItems = from c in listFields where c.GridShow.ToMyString() == "1" orderby c.GridSeq select c;
                table.ListShow = gridShowItems.ToList<EciField>();


                var editShowItems = from c in listFields where c.EditShow.ToMyString() == "1" orderby c.EditSeq select c;
                table.ListEdit = editShowItems.ToList<EciField>();

                // 获取 ECI_TABLE_CONTROL 扩展控件
                table.ListControl = controlBuilder.GetEntities(dtControl);

                //获取ECI_FIELD_EXTEND
                table.ListFieldExtend = new List<EciFieldExtend>();

                //获取ECI_TABLE_FUNCTION
                table.ListFunction = functionBuilder.GetEntities(dtFunction);


                // 获取 ECI_TABLE_DATA_SOURCE 数据源
                table.ListDataSource = dataSourceBuilder.GetEntities(dtDataSource);

                //获取 ECI_TABLE_CONFIRM 数据源
                table.ListConfirm = confirmBuilder.GetEntities(dtConfirm);

                //获取 ECI_TABLE_GROUP 数据源
                table.ListGroup = groupBuilder.GetEntities(dtGroup);
            }
            return table;
        }

        private static DataSet GetDataSetFromXML(string code)
        {
            DataSet ds = new DataSet();
            string fileName = PageHelper.ServerPath + "\\DEV_FACTORY\\" + code + ".xml";
            ds.ReadXml(fileName, XmlReadMode.ReadSchema);
            return ds;
        }
    }
}
